<?php
namespace app\common\model;
use app\common\model\Base;
use \think\Db;
class ConfigSet extends Base{
	protected $smsRecharge = 'SmsRechargeLog';//短信购买记录表
	protected $configSet = 'configSet';//配置表
	protected $smsEmailSet = 'SmsEmailSet';//邮箱配置表
	protected $smsSet = 'smsSet';//短信配置
	protected $smsTemplate = 'SmsTemplate';//短信模板
	protected $language = 'Language';//语言版本
	protected $webSet = 'WebSet';//网站设置
	protected $serList = 'ServiceList';//服务
	//获取邮箱配置参数
	public function get_email_config(){
		$config = db::connect('database.db_config2')->name($this->configSet)->select();
		foreach($config as $key=>$val){
			$get_config[$val['key']] =$val['value'];
		}
		return $get_config;
	}
	/*
	 * 获取短信配置
	 */
	public function get_sms(){
		$list = db::connect('database.db_config2')->name($this->smsSet)->where(1)->select();
		foreach($list as $k=>$v){
			$result[$v['k']] = $v['v'];
		}
		return $result;
	}
	/**
	 *获取短信条数
	 */
	public function check_sms(){
		// 2018-02-09 liiektnoo 短信条数检测暂时去掉
		// $sms_num = db::connect('database.db_config2')->name($this->serList)->where(['web_id'=>config('web_id'),'menu_action'=>7,'status'=>1])->value('times');
		$sms_num = 999;
		return $sms_num??0;
	}
	/*
	 * 获取短信模板列表
	 */
	public function get_sms_temp($wheresql='1'){
		//短信模板
		$list = db::connect('database.db_config2')->name($this->smsTemplate)->where($wheresql)->select();
		//语言版本
		$lang_list = $this->get_list("is_delete=0");
		foreach($lang_list as $kl=>$vl){
			$language[$vl['id']] = $vl;
		}
		foreach($list as $key => $value){
			$value['type_name'] = '';
			$value['language_name'] = $language[$value['lang_id']]['lang']??'';
			$list[$key] = $value;
		}
		return $list;
	}


	/*
	 * 获取短信发送记录
	 */
	public function get_sms_log($wheresql='1',$order="",$rows="",$field="*"){
		try {
        	$param = \app\common\model\Base::list_param('id','desc');
			$order = $order ?: $param ['sort'] . ' ' . $param ['order'];
			//print_r($param);
			$list['total'] = db('SmsLog')->where($wheresql)->count();
			$list['list'] = db('SmsLog')->where($wheresql)->field($field)->order($order)->page($param['page'],$param['rows'])->select();

			foreach($list['list'] as $key => $value){
				$value['status_name'] = $value['status']==1?'发送成功':'发送失败';
				$value['status_remark'] = $value['status']==1?$value['status_name']:$value['status_name'].": ".$value['remark'];
				$list['list'][$key] = $value;
			}
			$result['status'] = 1;
			$result['data'] = $list;
			return $result;
		} catch (\Exception $e) {
			$result['status'] = 0;
			$result['msg'] = $e;
			return $result;
		}
	}

	public function clear_sms_log($where = []){
		$ok = db('SmsLog')->where($where)->delete();
		if($ok){
			$result['msg'] = lang('SUCCESS_DONE');
			$result['status'] = 1;
		}else{
			$result['msg'] = lang('FAIL_DONE');
			$result['status'] = 0;
		}
		return $result;
	}


	/*
	 * 短信发送记录
	 */
	public function set_sms_log($option, $where = [], $data= []){
		if($option=='del'){
			$ok = Db::connect(config('database.db_config1'))->name('SmsLog')->where($where)->update($data);
		}else if($option=='add'){
			$ok =Db::connect(config('database.db_config1'))->name('SmsLog')->insert($data);
		}
		if($ok){
			$result['data'] = $data;
			$result['msg'] = '更新成功';
			$result['status'] = 1;
		}else{
			$result['data'] = [];
			$result['msg'] = '更新失败';
			$result['status'] = 0;
		}
		return $result;
	}

	//获取语言设置
	public function get_list($wheresql='1'){
		$list = db::connect('database.db_config2')->name($this->language)->where($wheresql)->select();
		return $list;
	}

	//短信充值
	public function sms_recharge($param){
		try{
			$db = $this->db();
			$db->name($this->smsRecharge)->insert($param);
		}catch (\Exception $e){
			return false;
		}
	}
	//充值列表
	public function sms_recharge_list($data,$param){
		try{
			$result['unit_price'] = $this->get_sms()['unit_price'];
			$result['list'] = db::name($this->smsRecharge)->field('id,web_id,lang_id,cast(unit_price/100 as decimal(10,2)) as unit_price,num,order_no,cast(price/100 as decimal(10,2)) as price,status,payment_type,cast(total_free/100 as decimal(10,2)) as total_free,addtime,pay_time')
					->where(['web_id'=>$data['web_id'],'lang_id'=>$data['lang_id']])
					->page($param['page'],$param['rows'])
					->order($param['order_str'])
					->select();
			$result['total'] = db::name($this->smsRecharge)
					->where(['web_id'=>$data['web_id'],'lang_id'=>$data['lang_id']])
					->count();
			return $result;
		}catch (\Exception $e){
			return false;
		}
	}
	//获取一条短信
    public function  sms_detail($param){
		try{
			//$result['unit_price'] = $this->get_sms()['unit_price'];
			$result = db::name($this->smsRecharge)->field('id,web_id,lang_id,cast(unit_price/100 as decimal(10,2)) as unit_price,num,order_no,cast(price/100 as decimal(10,2)) as price,status,payment_type,cast(total_free/100 as decimal(10,2)) as total_free,addtime,pay_time')
					->where(['web_id'=>config('web_id'),'lang_id'=>config('lang_id'),'id'=>intval($param['id'])])
					->find();
			return $result;
		}catch (\Exception $e){
			return false;
		}
	}

	//读取邮箱设置
	public function get_email_set($param,$where){
		try{
			$db = $this->db();
			$res= $db->name($this->smsEmailSet)->where($where)->find();
			if(empty($res)){
				$data = $this->get_email_config();
				$save = [
					'web_id'=>$param['web_id'],
					'lang_id'=>$param['lang_id'],
					'is_open'=>0,
					'email_account'=>$data['account'],
					'pass'=>$data['password'],
					'nickname'=>$data['from_name'],
					'smtp'=>$data['host'],
					'port'=>$data['port'],
					'addtime'=>time(),
				];
				$ok= $db->name($this->smsEmailSet)->insert($save);
				//print_r($data);
				return $save;
			}else{
				return $res;
			}
		}catch (\Exception $e){
			return false;
		}
	}


}